package com.zzyl.controller.customer;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.controller.BaseController;
import com.zzyl.dto.ReservationDto;
import com.zzyl.service.ReservationService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.ReservationVo;
import com.zzyl.vo.TimeCountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apiguardian.api.API;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/customer/reservation")
@Api(tags = "客户预约管理")
public class CustomerReservationController extends BaseController {
    @Autowired
  private ReservationService reservationService;


// 2.1 查询当天每个时间段取消预约的数量
    @GetMapping("/cancelled-count")
    @ApiOperation("2.1查询取消预约数量")
    public ResponseResult<Integer> getCancelledReservationCount() {
        Long userId = UserThreadLocal.getUserId();
        System.out.println(userId+"---------------------");
        return success(reservationService.getCancelledReservationCount(userId));
    }
// 2.2 查询每个时间段剩余预约次数
@GetMapping("/countByTime")
@ApiOperation("2.2查询每个时间段剩余预约次数")
public ResponseResult getCountByEachTime(Long time) {
    List<TimeCountVo> timeCountVoList = reservationService.getCountByEachTime(time);
    return success(timeCountVoList);
}

// 2.3 新增预约
    @PostMapping
    @ApiOperation("2.3 新增预约")
    public ResponseResult addReservation(
           @RequestBody ReservationDto reservationDto) {

        reservationService.addReservation(reservationDto);
        return success();
    }




    // 2.4 分页查询预约
    @GetMapping("/page")
    @ApiOperation("分页查询预约")
    public ResponseResult<PageResponse<ReservationVo>> findByPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                                                  @RequestParam(defaultValue = "10") Integer pageSize,
                                                                  Integer status) {
        Long userId = UserThreadLocal.getUserId();
        PageResponse<ReservationVo> reservationVoPageResponse = reservationService.findByPage(pageNum, pageSize, status,userId);
        return success(reservationVoPageResponse);
    }


// 2.5 查询所有预约:TODO
@GetMapping
@ApiOperation("查询所有预约")
public ResponseResult<List<ReservationVo>> findAll(@RequestParam(required = false) String mobile, @RequestParam(required = false) Long time) {
    List<ReservationVo> reservationVoList = reservationService.findAll(mobile, LocalDateTimeUtil.of(time));
    return success(reservationVoList);
}


// 2.6 取消预约
    @PutMapping("{id}/cancel")
    @ApiOperation("2.6 取消预约")
    public ResponseResult cancelReservation(@PathVariable Long id) {;
        reservationService.cancelReservation(id);
        return success();
    }

// 2.7 更新预约:TODO
    @PutMapping
    @ApiOperation("2.7 更新预约")
    public ResponseResult updateReservation(
            @RequestBody ReservationDto reservationDto) {
        Long userId = UserThreadLocal.getUserId();
       // reservationService.updateReservation(reservationDto, userId);
        return success();
    }



}